package Presenter;


import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import Model.Player;
import Model.Planet.Coordinate;
import Model.Planet.Planet;
import Model.Planet.Planet.CR;
import Model.Planet.Planet.IE;
import Model.Planet.Planet.Resource;
import Model.Planet.Planet.TechLevel;
import Run.Engine;
import View.GUI;
import View.LoadMenuView;
import View.LoadView;


public class LoadMenuPresenter{

	
	public LoadMenuPresenter(){
		
	}
	
	public LoadMenuView getView(){
		return new LoadMenuView();
	}
	
	public static void generatePlanets(){
		TechLevel level;
		Resource resource;
		IE ie;
		CR cr;
		int x;
		int y;
		final int radius;
		
		final List<Planet> list = new LinkedList<Planet>();
		final Random rand = new Random();
		final String[] solarSystemName = Planet.SolarSystemName;

		for(int count = 0; count < 25; count++){
		    //randomizes the planet's values
			level = Planet.levels[rand.nextInt(Planet.levels.length)];
			resource = Planet.resources[rand.nextInt(Planet.resources.length)];
			ie = Planet.ieList[rand.nextInt(Planet.ieList.length)];
			cr = Planet.crList[rand.nextInt(Planet.crList.length)];

			int num = rand.nextInt(solarSystemName.length);
			
			while(solarSystemName[num] == null){
				num = rand.nextInt(solarSystemName.length);
			}
			
			//instantiates a planet 
			Planet planet = new Planet(solarSystemName[num], level, resource, ie, cr);
			
			x = rand.nextInt(420);
			y = rand.nextInt(280);
			
			boolean run = true;
			
			while(run){
				run = false;
				for(Planet p : list){
					double dis = Engine.distance(x, y, p);
					
					if(dis < 60){
						run = true;
						x = rand.nextInt(420);
						y = rand.nextInt(280);
						break;
					}
				}
			}
			
			planet.setCoord(new Coordinate(x, y ));
			list.add(planet);
			solarSystemName[num] = null;
		}
		
		for(int count = 0; count < list.size(); count++){
			Engine.planets.put(list.get(count).getName(), list.get(count));
		}
		
		Player.getInstance().setCurrentPlanet(list.get(0));
	}

	public void newButton(){
		
		final GUI frame = GUI.getInstance();
		frame.setContentPane(new ConfigScreenPresenter().getView(), 420, 310);
		generatePlanets();
	}
	
	public void loadButton(){
		final GUI frame = GUI.getInstance();
		frame.setContentPane(new LoadView(), 420, 310);
		
	}
	
	public String toString(){
		return super.toString();
	}


}
